package zero.tech.core.event;

import java.util.HashMap;
import java.util.Map;

import zero.tech.core.find.DynamicFind;

@SuppressWarnings("rawtypes")
public class EventListenerManager extends DynamicFind {
	private EventListenerManager() {
	}

	private static EventListenerManager callBackListenerManager = new EventListenerManager();

	public static EventListenerManager getManager() {
		return callBackListenerManager;
	}

	private final Map<EventType, EventListener> netEventListeners = new HashMap<EventType, EventListener>();
	private final Map<EventType, EventCallbackListener> callBackListeners = new HashMap<EventType, EventCallbackListener>();

	@Override
	public void afterFind() {

	}

	@Override
	public void beforeFind() {

	}

	@Override
	public boolean verification(Class<?> clazz) {
		return interfaceOn(clazz, EventListener.class) || interfaceOn(clazz, EventCallbackListener.class);
	}

	@Override
	public void findClass(Class<?> clz) throws Exception {
		if (interfaceOn(clz, EventListener.class)) {
			EventListener netEventListener = (EventListener) clz.newInstance();
			netEventListeners.put(netEventListener.netEventType(), netEventListener);
		} else {
			EventCallbackListener netCallbackEventListener = (EventCallbackListener) clz.newInstance();
			callBackListeners.put(netCallbackEventListener.netEventType(), netCallbackEventListener);
		}
	}

	public EventListener getNetEventListener(EventType netEventType) {
		return netEventListeners.get(netEventType);
	}

	public EventCallbackListener getNetCallbackEventListener(EventType netEventType) {
		return callBackListeners.get(netEventType);
	}
}
